<?php


namespace app\index\controller;

use think\Db;
use think\exception\DbException;

/**
 * 数据库链式操作
 * Class DbChainTest
 * @package app\index\controller
 */
class DbChainTest
{
    /**
     * 链式查询where
     * sql：SELECT * FROM `t_user` WHERE  `age` = 40  AND `name` = '康辉'
     * @return \think\response\Json
     */
    public function chain_where_test()
    {
        try {
            $res = Db::table('t_user')
                ->where('age', 40)
                ->where('name', '康辉')
                ->select();
            return json(['msg' => '查询成功！', 'data' => $res]);
        } catch (DbException $e) {
            return json(['msg' => '查询失败！']);
        }
    }

    /**
     * 链式查询where 数组条件
     * sql：SELECT * FROM `t_user` WHERE  `age` = 40  AND `name` = '周杰伦001'
     * @return \think\response\Json
     */
    public function chain_where_arr_test()
    {
        try {
            $res = Db::table('t_user')
                ->where(['age'=>40,'name'=>'周杰伦001'])
                ->select();
            return json(['msg' => '查询成功！', 'data' => $res]);
        } catch (DbException $e) {
            return json(['msg' => '查询失败！']);
        }
    }
}